package com.adcc.ags.gms.mq.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.core.util.MQFactory;
import com.adcc.ags.gms.mq.vo.ChannelVO;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * MQ通道服务层
 */
@Transactional
@Service("ChannelService")
public class ChannelServiceImpl implements ChannelService {

    // 常量
    private final static String CHANNEL_NAME = "CHL.*";

    @Autowired
    private LogFactory log;

    @Autowired
    private MQFactory mqFactory;

    @Override
    public Optional<List<ChannelVO>> findAllChannel() {
        try {
            List<com.adcc.utility.mq.entity.ibm.Channel> listChannel = mqFactory.getSourceMQM().findChannel(CHANNEL_NAME);
            List<ChannelVO> list = new ArrayList<>();
            for (com.adcc.utility.mq.entity.ibm.Channel channel : listChannel) {
                ChannelVO channelVO = new ChannelVO();
                channelVO.setName(channel.getName());
                if (channel.getUser().equals("null") || channel.getUser().equals("")) {
                    channelVO.setUserId("");
                } else {
                    channelVO.setUserId(channel.getUser());
                }
                if (channel.getType() == 7) {
                    channelVO.setTypeName("服务器连接通道");
                } else if (channel.getType() == 1) {
                    channelVO.setTypeName("发送方通道");
                } else if (channel.getType() == 2) {
                    channelVO.setTypeName("服务器方通道");
                } else if (channel.getType() == 3) {
                    channelVO.setTypeName("接收方通道");
                } else if (channel.getType() == 4) {
                    channelVO.setTypeName("请求方通道");
                } else if (channel.getType() == 8) {
                    channelVO.setTypeName("集群发送方通道");
                } else if (channel.getType() == 9) {
                    channelVO.setTypeName("集群接收方通道");
                } else if (channel.getType() == 11) {
                    channelVO.setTypeName("AMQP");
                } else if (channel.getType() == 6) {
                    channelVO.setTypeName("客户端连接通道");
                } else {
                    channelVO.setTypeName("其他类型通道");
                }
                list.add(channelVO);
            }
            return Optional.of(list);
        } catch (Exception ex) {
            log.error(QueueService.class,"MQ",GWLogLevel.SERIOUS_ALARM, "获取通道信息异常", ex);
            return Optional.absent();
        }
    }
}
